﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ParallelArrays;
using FPA = Microsoft.ParallelArrays.FloatParallelArray;
using BPA = Microsoft.ParallelArrays.BoolParallelArray;
using IPA = Microsoft.ParallelArrays.IntParallelArray;
using PA = Microsoft.ParallelArrays.ParallelArrays;

namespace CoreCalc.SheetDefinedFunctions.Accellerator
{
    public class RandAccNode : AccExpr
    {
        Random rnd;
        
        public enum Type { RAND, RANDBETWEEN };
        Type type;

        AccExpr min, max;

        public RandAccNode(Type t)
        {
            rnd = new Random();
            type = t;
        }

        public RandAccNode(AccExpr min, AccExpr max) : this(Type.RANDBETWEEN)
        {
            this.min = min;
            this.max = max;
        }

        protected override FPA GenerateFPA(AccInputInfo info, int CallID)
        {
           
            if (type==Type.RAND)
            {
                float[,] cValues = new float[info.Values[0].GetLength(0), info.Values[0].GetLength(1)];
                for (int i = 0; i < cValues.GetLength(0); i++)
                    for (int j = 0; j < cValues.GetLength(1); j++)
                    cValues[i, j] = (float)rnd.NextDouble();
                return new FPA(cValues);
            }
            else
            {
                float[,] cValues = new float[info.Values[0].GetLength(0), info.Values[0].GetLength(1)];
                for (int i = 0; i < cValues.GetLength(0); i++)
                    for (int j = 0; j < cValues.GetLength(1); j++)
                        cValues[i, j] = (float)rnd.NextDouble();

                var maxFPA = max.GenerateFPAWithCache(info, CallID);
                var maxAndOneFPA = PA.Add(1, maxFPA);
                var vFPA = PA.Floor((PA.Multiply(new FPA(cValues), maxAndOneFPA)));
                return PA.Cond(PA.CompareEqual(vFPA, maxAndOneFPA), maxFPA, vFPA);
            }


        }
    }
}
